#include "TeamMenu.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "GamePrintUtils.h"
#include "ServiceProvider.h"
#include "TeamManager.h"
#include "PlayerMenu.h"
#include "TeamMatchesMenu.h"
#include "UserManager.h"
#include "User.h"
#include "UserSelectionMenu.h"
#include "ShortListMenu.h"
#include "TeamHistoryMenu.h"
#include "Types.h"

TeamMenu::TeamMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
	,m_editing(false)
	,m_swappingPlayer(0)
	,m_teamID(Utilities::INVALID_ID)
{
}

void TeamMenu::OnEnter()
{
	m_editing = false;
	m_swappingPlayer = 0;

	if(m_teamID == Utilities::INVALID_ID)
	{
		UserSelectionMenu* usMenu = static_cast<UserSelectionMenu*>(GetMM()->GetUserSelectionMenu());
		m_teamID = GetSP()->GetUserMgr().GetUserList().at( usMenu->GetUserSelected())->m_team->GetID();
	}
}

void TeamMenu::Draw() const
{
	STD::cout << Print::Title("TEAM MENU'");
	const Team* const team = GetSP()->GetTeamMgr().GetTeam(m_teamID);
	PrintTeam(STD::cout, *team);

	if (m_editing)
	{	
		STD::cout << "Choose " << ((m_swappingPlayer == 0) ? "first" : "second") << " player to swap (0 to abort) : ";
	}
	else
	{
		STL::string options("1-"+Utilities::IntToStlString(team->GetNumPlayers())+") People Info.\n");

		if(team->IsUserControlled())
			options += "33) Edit formation.\n";
		options += "44) View Season Matches.\n";
		options += "55) View Team History.\n";
		if(team->IsUserControlled())
		{
			options += "66) View Shortlist.\n";
			options += "77) Switch Formation.\n";
		}
		options += "\n0) Return to previous menu'.";
		STD::cout  << Print::Options(options) << "Selection: ";;
	}
}

bool TeamMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;
	Team* const team = GetSP()->GetTeamMgr().GetTeam(m_teamID);

	int res = Utilities::StringToInt(i_input);

	if (m_editing)
	{
		if (res == 0)
		{
			OnEnter();
			goodInput = false;
		}
		else
		{
			if (m_swappingPlayer == 0)
			{
				if (res > 0 && res <= static_cast<int>(team->GetNumPlayers()))
					m_swappingPlayer = res;
				else 
					goodInput = false;
			}
			else
			{
				if (res > 0 && res <= static_cast<int>(team->GetNumPlayers()))
				{
					team->SwapPlayers(m_swappingPlayer - 1, res - 1);
					m_editing = false;
					m_swappingPlayer = 0;
				}
				else 
					goodInput = false;
			}
		}
	}
	else
	{
		if (res > 0 && res <= static_cast<int>(team->GetNumPlayers()))
		{
			PlayerMenu* nextMenu = static_cast<PlayerMenu*>(GetMM()->GetPlayerMenu());
			nextMenu->SetPlayer(team->GetPlayersList()[res-1]);
			GetMM()->PushMenu(nextMenu);
		}
		else if (res == 0)
		{
			GetMM()->PopMenu();
		}
		else if (res == 44)
		{
			TeamMatchesMenu* nextmenu = static_cast<TeamMatchesMenu*>(GetMM()->GetTeamMatchesMenu());
			nextmenu->SetTeamID(m_teamID);
			GetMM()->PushMenu(nextmenu);
		}
		else if (res == 55)
		{
			TeamHistoryMenu* nextmenu = static_cast<TeamHistoryMenu*>(GetMM()->GetTeamHistoryMenu());
			nextmenu->SetTeam(m_teamID);
			GetMM()->PushMenu(nextmenu);
			goodInput = false;
		}
		else if (team->IsUserControlled())
		{
			if (res == 33)
			{
				m_editing = true;
			}
			else if (res == 66)
			{
				ShortListMenu* nextmenu = static_cast<ShortListMenu*>(GetMM()->GetShortListMenu());
				nextmenu->SetTeam(GetSP()->GetTeamMgr().GetTeam(m_teamID));
				GetMM()->PushMenu(nextmenu);
			}
			else if (res == 77)
			{
				const int currentFormation = GetSP()->GetTeamMgr().GetTeam(m_teamID)->GetFormation();
				const int nextFormation = (currentFormation+1) % NUM_TEAM_FORMATIONS;
				GetSP()->GetTeamMgr().GetTeam(m_teamID)->SetFormation(nextFormation);
			}
		}
	}
	return goodInput;
}
